package com.zxp.security.controller;

import cn.hutool.core.util.RandomUtil;
import com.zxp.common.core.validatecode.impl.ImageValidateCodeService;
import com.zxp.common.core.validatecode.impl.SmsValidateCodeService;
import com.zxp.common.core.validatecode.sms.SmsSenderApi;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;

/**
 * @author shollin
 * @desc
 * @date 2021/7/8/008 13:17
 */
@Controller
@RequiredArgsConstructor
@Slf4j
public class ValidateCodeController {

    private final RedisTemplate<String,Object> redisTemplate;
    private final ImageValidateCodeService imageValidateCodeService;
    private final SmsValidateCodeService smsValidateCodeService;

    /**
     * 前后端app无cookie的项目，会带上uuid身份， 前后端分离的，将uuid设置到cookie值中
     * @param uuid
     * @param width
     * @param height
     * @param len
     * @param type
     * @param request
     * @param response
     * @throws Exception
     */
    @GetMapping("/images/captcha")
    @SneakyThrows
    public void captcha(
            @RequestParam(required = false) String uuid,
            @RequestParam(defaultValue = "80") Integer width,
            @RequestParam(defaultValue = "24") Integer height,
            @RequestParam(defaultValue = "4") Integer len,
            @RequestParam(defaultValue = "1") Integer type,
            HttpServletRequest request, HttpServletResponse response) throws Exception{

        // 默认会保存到session当中 SESSION_KEY = "captcha";
        imageValidateCodeService.sendCode(new ServletWebRequest(request,response));

    }

    @GetMapping("/code/sms")
    @SneakyThrows
    public void smsCode(@RequestParam(required = true) String phone,HttpServletRequest request, HttpServletResponse response){
        smsValidateCodeService.sendCode(new ServletWebRequest(request,response));
    }

}
